package com.example.demo.controller;

import com.example.demo.mapper.MusicMapper;
import com.example.demo.pojo.Music;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @Autowired
    MusicMapper musicMapper;

//    @RequestMapping("/upload")
//    public String upload(){
//        return "upload";
//    }

    @RequestMapping(value = "/music", method = RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody Music music){
        Map<String,Object>map = new HashMap<>();
        if(music!=null){
            File musicFile = new File("src"+File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"mp3"+File.separator+music.getMusicPath());
            File imgFile = new File("src"+File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"img"+File.separator+music.getImgPath());
            if(musicFile.exists() && imgFile.exists()){
                int t = musicMapper.add(music);
                System.out.println(t);
                map.put("code",0);
                map.put("msg","ok");
            }
            else{
                map.put("code",1);
                map.put("msg","file not exists");
            }
        }
        else{
            map.put("code",1);
            map.put("msg","error");
        }
        //System.out.println(music);
        return map;
    }
}
